/*
 * test.c
 *
 * Created: 9/24/2013 5:22:06 PM
 *  Author: Tim
 */ 
#include "test.h"

void seedADCInit(uint8_t ACHM,uint8_t GAIN)
{
	adcInit_t packet;
	uint8_t *ptr;
	ptr = &packet;
	
	packet.Header		= packetHeader;
	packet.msgHeader	= adcInit_mh;
	
	packet.PARAM1.ACHM = ACHM;
	packet.PARAM1.GAIN = GAIN>>ADC_CH_GAIN_gp;
	
	crcAppend(&packet,packetSizeLookup(adcInit_mh));
	
	uint8_t i;
	for(i=0;i<packetSizeLookup(adcInit_mh);i++) bufferWrite(&bufRxD1,*(ptr++));
}


void seedDacEnable(uint8_t DCHM)
{
	dacEnable_t packet;
	
	uint8_t *ptr;
	ptr = &packet;
	
	packet.Header		= packetHeader;
	packet.msgHeader	= dacEnable_mh;

	packet.PARAM1.DCHM	= DCHM;		
	
	crcAppend(&packet,packetSizeLookup(dacEnable_mh));
	
	uint8_t i;
	for(i=0;i<packetSizeLookup(dacEnable_mh);i++) bufferWrite(&bufRxD1,*(ptr++));
}

void seedDigitalInEnable()
{
	digitalInEnable_t packet;
	
	uint8_t *ptr;
	ptr = &packet;
	
	packet.Header		= packetHeader;
	packet.msgHeader	= digitalInEnable_mh;
	
	crcAppend(&packet,packetSizeLookup(digitalInEnable_mh));
	
	uint8_t i;
	for(i=0;i<packetSizeLookup(digitalInEnable_mh);i++) bufferWrite(&bufRxD1,*(ptr++));
}

void seedDigitalOutEnable()
{
	digitalInEnable_t packet;
	
	uint8_t *ptr;
	ptr = &packet;
	
	packet.Header		= packetHeader;
	packet.msgHeader	= digitalOutEnable_mh;
	
	crcAppend(&packet,packetSizeLookup(digitalOutEnable_mh));
	
	uint8_t i;
	for(i=0;i<packetSizeLookup(digitalOutEnable_mh);i++) bufferWrite(&bufRxD1,*(ptr++));
}

void seedEncInit(uint8_t ENCN,uint8_t INDX,uint8_t PLOC,uint8_t ABS,uint8_t EDFL, uint16_t ERES)
{
	encInit_t packet;
	
	uint8_t *ptr;
	ptr = &packet;
	
	WORD_t RES;
	
	RES.word = ERES;
	
	packet.Header		= packetHeader;
	packet.msgHeader	= encInit_mh;
	
	packet.PARAM1.ENCN	= ENCN;
	packet.PARAM1.INDX	= INDX;	
	packet.PARAM1.PLOC	= PLOC;
	
	packet.PARAM2.ABS	= ABS;
	packet.PARAM2.EDFL	= EDFL;
	packet.PARAM2.ERESH	= RES.bytes.byteH;
	
	packet.PARAM3.ERESL	= RES.bytes.byteL;
	
	crcAppend(&packet,packetSizeLookup(encInit_mh));
	
	uint8_t i;
	for(i=0;i<packetSizeLookup(encInit_mh);i++) bufferWrite(&bufRxD1,*(ptr++));
}

void seedTmrSetFreq(uint16_t freq)
{
	tmrSetFreq_t packet;
	
	uint8_t *ptr;
	ptr = &packet;
	
	packet.Header		= packetHeader;
	packet.msgHeader	= tmrSetFreq_mh;
	
	packet.frequency = freq;
	
	crcAppend(&packet,packetSizeLookup(tmrSetFreq_mh));
	
	uint8_t i;
	for(i=0;i<packetSizeLookup(tmrSetFreq_mh);i++) bufferWrite(&bufRxD1,*(ptr++));
}

void seedServoEnable(uint8_t enables[4])
{
	servoEnable_t packet;
	
	uint8_t *ptr;
	ptr = &packet;
	
	packet.Header		= packetHeader;
	packet.msgHeader	= servoEnable_mh;
	
	packet.PARAM1.servoEn_msk0 = enables[0];
	packet.PARAM1.servoEn_msk1 = enables[1];
	packet.PARAM1.servoEn_msk2 = enables[2];
	packet.PARAM1.servoEn_msk3 = enables[3];
	
	
	crcAppend(&packet,packetSizeLookup(servoEnable_mh));
	
	uint8_t i;
	for(i=0;i<packetSizeLookup(servoEnable_mh);i++) bufferWrite(&bufRxD1,*(ptr++));	
}

void seedMotorEnable(uint8_t enables[4])
{
	motorEnable_t packet;
	
	uint8_t *ptr;
	ptr = &packet;
	
	packet.Header		= packetHeader;
	packet.msgHeader	= motorEnable_mh;
	
	packet.PARAM1.motorEn_msk0 = enables[0];
	packet.PARAM1.motorEn_msk1 = enables[1];
	packet.PARAM1.motorEn_msk2 = enables[2];
	packet.PARAM1.motorEn_msk3 = enables[3];
	
	
	crcAppend(&packet,packetSizeLookup(motorEnable_mh));
	
	uint8_t i;
	for(i=0;i<packetSizeLookup(motorEnable_mh);i++) bufferWrite(&bufRxD1,*(ptr++));	
}
	
void seedOutputSuperPacket(uint16_t analogOut[],uint16_t digSet, uint16_t digClr)
{
	WORD_t val;
	
	uint8_t packet[32];
	uint8_t i;
	
	uint8_t *ptr;
	ptr = &packet;
	
	packet[i++] = packetHeader;
	packet[i++] = outputSuperPacket_mh;
	
	if(daqAlloc.analogAlloc.analogOutCh0Alloc)
	{
		val.word = analogOut[0];
		packet[i++] = val.bytes.byteH;
		packet[i++] = val.bytes.byteL;
	}
	if(daqAlloc.analogAlloc.analogOutCh1Alloc)
	{
		val.word = analogOut[1];
		packet[i++] = val.bytes.byteH;
		packet[i++] = val.bytes.byteL;
	}
	
	if(daqAlloc.digitalOut)
	{
		val.word = digSet;
		packet[i++] = val.bytes.byteH;
		packet[i++] = val.bytes.byteL;
		
		val.word = digClr;
		packet[i++] = val.bytes.byteH;
		packet[i++] = val.bytes.byteL;
	}

	crcAppend(&packet[0],packetSizeLookup(outputSuperPacket_mh));
	for(i=0;i<packetSizeLookup(outputSuperPacket_mh);i++) bufferWrite(&bufRxD1,*(ptr++));	
}

void seedActuatorSuperPacket(uint16_t motorVal[],uint16_t servoVal[])
{
	WORD_t val;
	
	uint8_t packet[32];
	uint8_t i = 0;

	uint8_t *ptr;
	ptr = &packet;
	
	packet[i++] = packetHeader;
	packet[i++] = actuatorSuperPacket_mh;
	
	if(daqAlloc.actuatorAlloc.servo0Alloc == FEATURE_UTILIZED)
	{
		val.word = servoVal[0];
		packet[i++] = val.bytes.byteH;
		packet[i++] = val.bytes.byteL;
	}
	if(daqAlloc.actuatorAlloc.servo1Alloc == FEATURE_UTILIZED)
	{
		val.word = servoVal[1];
		packet[i++] = val.bytes.byteH;
		packet[i++] = val.bytes.byteL;
	}
	
	if(daqAlloc.actuatorAlloc.servo2Alloc == FEATURE_UTILIZED)
	{
		val.word = servoVal[2];
		packet[i++] = val.bytes.byteH;
		packet[i++] = val.bytes.byteL;
	}
	if(daqAlloc.actuatorAlloc.servo3Alloc == FEATURE_UTILIZED)
	{
		val.word = servoVal[3];
		packet[i++] = val.bytes.byteH;
		packet[i++] = val.bytes.byteL;
	}
	
	if(daqAlloc.actuatorAlloc.motor0Alloc == FEATURE_UTILIZED)
	{
		val.word = motorVal[0];
		packet[i++] = val.bytes.byteH;
		packet[i++] = val.bytes.byteL;
	}
	if(daqAlloc.actuatorAlloc.motor1Alloc == FEATURE_UTILIZED)
	{
		val.word = motorVal[1];
		packet[i++] = val.bytes.byteH;
		packet[i++] = val.bytes.byteL;
	}
	if(daqAlloc.actuatorAlloc.motor2Alloc == FEATURE_UTILIZED)
	{
		val.word = motorVal[2];
		packet[i++] = val.bytes.byteH;
		packet[i++] = val.bytes.byteL;
	}
	if(daqAlloc.actuatorAlloc.motor3Alloc == FEATURE_UTILIZED)
	{
		val.word = motorVal[3];
		packet[i++] = val.bytes.byteH;
		packet[i++] = val.bytes.byteL;
	}
	
	crcAppend(&packet[0],packetSizeLookup(actuatorSuperPacket_mh));
	for(i=0;i<packetSizeLookup(actuatorSuperPacket_mh);i++) bufferWrite(&bufRxD1,*(ptr++));	
}
			